var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update }); 
var score = 0; 
var scoreText;
var cursors;
var timer = 0;
var total = 0;
var facing= 'left';
var scale = 0;
var lives = 3;

var emitter;



function preload() 
{ 

	game.load.image('sky', 'assets/bg.png'); 
	game.load.image('ground', 'assets/test.png'); 
	game.load.image('star', 'assets/coin.gif'); 
	game.load.spritesheet('dude', 'assets/unit.png', 32, 48); 
	game.load.spritesheet('baddie', 'assets/baddie.png', 32, 32);
	game.load.audio('musica', 'assets/03-mb-stage-theme-1.mp3');

}; 

function create() 
{ 
		 music = game.add.audio('musica');
		// A simple background for our game 
		game.add.sprite(-450, -0, 'sky'); 
        emitter = game.add.emitter(game.world.centerX, game.world.centerY, 250);

    emitter.makeParticles('baddie', [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20], 200, true, true);

    emitter.minParticleSpeed.setTo(-200, -300);
    emitter.maxParticleSpeed.setTo(200, -400);
    emitter.gravity = 150;
    emitter.bounce.setTo(0.2, 0.2);
    emitter.angularDrag = 30;

    emitter.start(false, 8000, 400);




					// The platforms group contains the ground and the 2 ledges we can jump on 
					platforms = game.add.group();
 
								// Here we create the ground. 
								var ground = platforms.create(0, game.world.height-40, 'ground'); 
						


								
								
									
											// Scale it to fit the width of the game (the original sprite is 400x32 in size)
											ground.scale.setTo(2, 2); 

																		// This stops it from falling away when you jump on it 
																		ground.body.immovable = true; 
																		/*ground1.body.immovable = true;
																		ground2.body.immovable = true;
																		ground3.body.immovable = true;
																		ground4.body.immovable = true;
																		ground5.body.immovable = true;
																		ground6.body.immovable = true;
																		ground7.body.immovable = true;
																		ground8.body.immovable = true; */
																		
						// Now let's create two ledges 
							var ledge = platforms.create(400, 400, 'ground'); 
						    ledge.body.immovable = true;
							ledge = platforms.create(180, 250, 'ground'); 
							ledge.body.immovable = true;
							ledge = platforms.create(0, 100, 'ground'); 
							ledge.body.immovable = true;
							ledge = platforms.create(100, 170, 'ground'); 
							ledge.body.immovable = true;
						    ledge = platforms.create(700, 150, 'ground'); 
							ledge.body.immovable = true;
							ledge = platforms.create(550, 300, 'ground'); 
							ledge.body.immovable = true;
																						
									
							ledge = platforms.create(0, game.world.height-40, 'ground'); 
							ledge.body.immovable = true;
							ledge = platforms.create(100, game.world.height-40, 'ground'); 
							ledge.body.immovable = true;
							ledge = platforms.create(200, game.world.height-40, 'ground'); 
							ledge.body.immovable = true;
							ledge = platforms.create(300, game.world.height-40, 'ground'); 
							ledge.body.immovable = true;
							ledge = platforms.create(400, game.world.height-40, 'ground'); 
							ledge.body.immovable = true;
							ledge = platforms.create(500, game.world.height-40, 'ground'); 
							ledge.body.immovable = true;
							ledge = platforms.create(600, game.world.height-40, 'ground'); 
							ledge.body.immovable = true;
							ledge = platforms.create(700, game.world.height-40, 'ground'); 
							ledge.body.immovable = true;								
																								
					// The player and its settings 
							player = game.add.sprite(32, game.world.height - 150, 'dude'); 
						
							
							baddies = game.add.group(); 
								
			for (var i = 0; i < Math.random()*10; i++) 
			{
 													
		baddy = baddies.create(i * 70, 0, 'baddie');
		
		baddy.body.bounce.y = 0.2; 
		baddy.body.gravity.y = 6; 
		baddy.body.collideWorldBounds = true; 
		
		baddy.animations.add('walk',[0,1],10,true);
		baddy.animations.play('walk',10, true);

	  
	  //Make baddy1 moving
	game.add.tween(baddy).to({ x: 750 }, 20000, Phaser.Easing.Linear.None)
	.to({ x: 1 }, 20000, Phaser.Easing.Linear.None)
	.loop()
	.start();
	
    total++;
    timer = game.time.now + 50;
		}
// Player physics properties. Give the little guy a slight bounce. 
player.body.bounce.y = 0.2; 
player.body.gravity.y = 6; 
player.body.collideWorldBounds = true; 





// Our two animations, walking left and right. 
player.animations.add('left', [0, 1, 2, 3], 10, true); 

player.animations.add('right', [5, 6, 7, 8], 10, true);


								


//defying gravity both units
player.body.gravity.y = 6;
//baddy.body.gravity.y = 6;

stars = game.add.group(); 


			// Here we'll create 12 of them evenly spaced apart 
			for (var i = 0; i < 12 ; i++) 
			{ 
				
				// Create a star inside of the 'stars' group 
				var star = stars.create(i * 70, 0, 'star'); 
				// Let gravity do its thing 
				star.body.gravity.y = 6; 
				// This just gives each star a slightly random bounce value 
				star.body.bounce.y = 0.7 + Math.random() * 0.2; 
			
			}

cursors = game.input.keyboard.createCursorKeys();

scoreText = game.add.text(16, 16, 'score: 0', { fontSize: '32px', fill: '#000' });

} 



function createbaddie()
{
	
	
	baddy = game.add.sprite((Math.random() * 600), game.world.height - 600, 'baddie');
	
	
	
	
	//baddie.body.immovable = true;
	baddy.body.bounce.y = 0.2; baddy.body.gravity.y = 6; baddy.body.collideWorldBounds = true; 
	baddy.body.checkCollision.up = false;
	baddy.body.checkCollision.down = false;

	baddy.animations.add('walk',[2,3],10,true);
	baddy.animations.play('walk',10, true);
	game.add.tween(baddy).to({ x: game.width + (1600 + baddy.x) }, 20000, Phaser.Easing.Linear.None, true);
	
	


	
	
	
    total++;
    timer = game.time.now + 50;
  /*  
    baddy1 = game.add.sprite((Math.random() * 600), game.world.height - 600, 'baddie');
	//baddie.body.immovable = true;
	baddy1.body.bounce.y = 0.2; baddy1.body.gravity.y = 6; baddy1.body.collideWorldBounds = true; 
	baddy1.animations.add('walk',[2,3],10,true);
	baddy1.animations.play('walk',10, true);
	game.add.tween(baddy1).to({ x: game.width + (1600 + baddy1.x) }, 20000, Phaser.Easing.Linear.None, true);
	
    total++;
    timer = game.time.now + 50;


	baddy2 = game.add.sprite((Math.random() * 600), game.world.height - 600, 'baddie');
	//baddie.body.immovable = true;
	baddy2.body.bounce.y = 0.2; baddy2.body.gravity.y = 6; baddy2.body.collideWorldBounds = true; 
	baddy2.animations.add('walk',[2,3],10,true);
	baddy2.animations.play('walk',10, true);
	game.add.tween(baddy2).to({ x: game.width + (1600 + baddy1.x) }, 20000, Phaser.Easing.Linear.None, true);
	
    total++;
    timer = game.time.now + 50;
	*/
}









						function update() 
						{
							
						 
  
							 
	                   
	
                       
						
						
						// Collide the player and the stars with the platforms 
						game.physics.collide(player, platforms);
						game.physics.collide(baddies, platforms);
						//game.physics.collide(baddy0, platforms);
						//game.physics.collide(baddy1, platforms);
						//game.physics.collide(baddy2, platforms);
						game.physics.collide(stars, platforms);
					game.physics.collide(player, baddies);
						
					//player will walk through the fake buddies	
					game.physics.collide(emitter);
                    game.physics.collide(platforms,emitter);
						
						
					
					// Reset the players velocity (movement) 
						player.body.velocity.x = 0; 
						
									if (cursors.left.isDown) 
									{
										 
										// Move to the left 
										player.body.velocity.x = -150; 
										player.animations.play('left');
									
									}
									else if (cursors.right.isDown) 
									{ 
										// Move to the right 
										player.body.velocity.x = 150; 
										player.animations.play('right');
									} 
									
									else 
									{ 
										// Stand still 
										player.animations.stop(); 
										player.frame = 4; 
									}
									
									// Allow the player to jump if they are touching the ground. 
									if (cursors.up.isDown && player.body.touching.down)
									{ 
										player.body.velocity.y = -350; 
									}
						
							game.physics.overlap(player, stars, collectStar, null, this);
						
							game.physics.overlap(player, baddies, hitBybaddie, null, this);
						
						}







function collectStar (player, star) 
{ 
// Removes the star from the screen 
star.kill(); 
// Add and update the score 
score += 10; 
scoreText.content = 'Score: ' + score;

if(score == 120)
{
	//you win
	var style = { font: "65px Arial", fill: "#ff0044", align: "center" };
    var text = game.add.text(game.world.centerX, game.world.centerY, "- phaser -\nwith a sprinkle of\npixi dust", style);

    text.anchor.set(0.5);
	
	//refresh screen
	location.reload();
}

}






function hitBybaddie (player, baddies)
{
player.kill();
location.reload();
}






	
	

function render () {

    game.debug.body(buddies);
    game.debug.body(player);


}
   